﻿package controllers 
{
	import events.UserEvent;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import model.MainModel;
	import model.User;
	import utils.VkQuery;
	
	/**
	 * ...
	 * @author Slavshik Alexander
	 */
	public class InfoController extends BaseController
	{
		[Event(name="onUserInfoLoaded", type="events.UserEvent")]
		[Event(name="onUserFriendsLoaded", type="events.UserEvent")]
		[Event(name="onUserFriendsInited", type="events.UserEvent")]
		
		public function init(obj:Object):void {
			MainModel.user.uid = int(obj.user_id);
			MainModel.viewer.uid = int(obj.viewer_id);
			//MainModel.user.uid = MainModel.viewer.uid = 5615762;
			//MainModel.user.uid = MainModel.viewer.uid = 5615762;
			MainModel.viewer.uid = 4264707;
			MainModel.user.uid = 5615762;
			var urlRequest:URLRequest = new URLRequest(VkQuery.getInfo([MainModel.user.uid, MainModel.viewer.uid]));
			trace(urlRequest.url)
			urlLoader.addEventListener(Event.COMPLETE, onGetInfo);
			VkQuery.loadStackQuery(urlLoader, urlRequest);
		}
		private function onGetInfo(e:Event):void {
			stopQueries();
			if (!checkXML(e.currentTarget.data)) return;
			var xml:XML = new XML(e.currentTarget.data);
			MainModel.user.fname = String(xml.user[0].first_name);
			MainModel.user.lname = String(xml.user[0].last_name);
			MainModel.user.ava = String(xml.user[0].photo);
			if (xml.user.length() > 1) {
				MainModel.viewer.fname = String(xml.user[1].first_name);
				MainModel.viewer.lname = String(xml.user[1].last_name);
				MainModel.viewer.ava = String(xml.user[1].photo);
			}
			dispatchEvent(new UserEvent(UserEvent.INITED));
		}
		public function getFriends():void {
			if (!MainModel.viewer.friends.length) {
				stopQueries();
				var urlRequest:URLRequest = new URLRequest(VkQuery.getFriends());
				urlLoader.addEventListener(Event.COMPLETE, onGetFriends);
				VkQuery.loadStackQuery(urlLoader, urlRequest);
			} else dispatchEvent(new UserEvent(UserEvent.FRIENDS_INITED));
		}
		private function onGetFriends(e:Event):void {
			stopQueries();
			if (!checkXML(e.currentTarget.data)) return;
			var xml:XML = new XML(e.currentTarget.data);
			var arr:Array = [];
			for (var i:int = 0; i < xml.children().length(); i++) arr.push(int(xml.children()[i]));
			var urlRequest:URLRequest = new URLRequest(VkQuery.getInfo(arr));
			urlLoader.addEventListener(Event.COMPLETE, onGetFriendsInfo);
			VkQuery.loadStackQuery(urlLoader, urlRequest);
			dispatchEvent(new UserEvent(UserEvent.FRIENDS_LOADED));
		}
		private function onGetFriendsInfo(e:Event):void {
			stopQueries();
			MainModel.viewer.friends = [];
			var friends:XMLList = new XML(e.currentTarget.data).children();
			for (var i:int = 0; i < friends.length(); i++) {
				var u:User = new User();
				u.uid = friends[i].uid
				u.fname = friends[i].first_name;
				u.lname = friends[i].last_name;
				u.ava = friends[i].photo;
				MainModel.viewer.friends.push(u);
			}
			dispatchEvent(new UserEvent(UserEvent.FRIENDS_INITED));
		}
		override public function stopQueries():void {
			super.stopQueries();
			urlLoader.removeEventListener(Event.COMPLETE, onGetFriends);
			urlLoader.removeEventListener(Event.COMPLETE, onGetFriendsInfo);
			urlLoader.removeEventListener(Event.COMPLETE, onGetInfo);
		}
		public function changeUser(user:User):void {
			MainModel.user = user;
		}
		
	}
	
}